﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using System;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YGRHRPDC : DocumentBase
    {
        #region-------------Properties-----------------------

        string sql = "";
        DataTable TResult = null; 
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string ABFAGANG = "";
        string ROWID = "";
        bool IsLoaded = false;
        DataRow CurrentRow = null;
        DataSet SResult = null;
        DataRow LastRow = null;
        DataSet SetMap = null;
        public DataSet dsResult;
        MainViewModel MainViewModel;

        public SETINTEXDB SetData
        {
            get
            {
                return MainViewModel.SetData;
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return MainViewModel.SetEnv;
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return MainViewModel.SetProd;
            }
        }
        CollectionViewSource vwTPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTPDC");
            }
        }

        SQLiteDatabase SQLiteDatabase
        {
            get
            {
                return MainViewModel.SQLiteDatabase;
            }
        }
        DataTable TBResult;
        #endregion
        public YGRHRPDC()
        {
            InitializeComponent();
            this.Name = "作业采集数据分析";
            if (INTEXDBC == null)
            {
                app.Load();
            }
            MainViewModel = new MainViewModel();
            DataContext = MainViewModel;
            EventDispatcher.Instance.OnStatusChange += Instance_OnStatusChange;
            txtMessage.Visibility = Visibility.Collapsed;
        }
        void Instance_OnStatusChange(object sender, StatusEventArgs e)
        {
            MessageInfo msg = ((StatusEventArgs)e).Info;
            string Message = msg.Details.Trim();
            if (Message != "")
            {
                txtMessage.Text = msg.Details;
                if (Message.Contains("YGRCPGB04"))
                {
                    txtMessage.Visibility = Visibility.Visible;
                }
                if (Message.Contains("YGRCPGB04启动"))
                {
                    txtMessage.Visibility = Visibility.Visible;
                }
                if (Message.Contains("YGRCPGB04完成"))
                {
                    txtMessage.Visibility = Visibility.Collapsed;
                }
                if (Message.Contains("YGRCPGB04异常终止"))
                {
                    txtMessage.Visibility = Visibility.Collapsed;
                }

            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = Helpers.Helper.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            txtLogIn.Background = new SolidColorBrush(Colors.Green);
            expLog.IsExpanded = false;
            expLog.Visibility = Visibility.Collapsed;
            gdMain.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (app.User.Trim() == "ITXV")
                {
                    txtUserID.Text = app.User;
                    txtPassword.Password = app.Pwd;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void PDCTYPE_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            ABFAGANG = (cb.SelectedItem as ComboBoxItem).Tag.ToString();
            switch (ABFAGANG.Trim())
            {
                case "XS":
                    MainViewModel.ABFAGANG = ABFAGANG; 
                    break;
                case "CS":
                    MainViewModel.ABFAGANG = ABFAGANG; 
                    break;
            }
        }

    }
}
